package com.qbq;

import com.qbq.monster.*;

import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;

/**
 * @description:
 * @author: QiaoBiQing
 * @date: Created in 2020/4/15 11:11
 * @version: 1.0
 * @modified By:
 */
public class Game {
    private Person person = new Person();
    private MonsterWolf wolf1 = new MonsterWolf();
    private MonsterWolf wolf2 = new MonsterWolf();
    private MonsterWolf wolf3 = new MonsterWolf();
    private MonsterWolfSet monsterWolfSet =new MonsterWolfSet();
    UI ui=new UI();
    public Room currentRoom;
    public HashMap<String,Handler> handles = new HashMap<String, Handler>();
    public Game()
    {
        handles.put("go",new HandlerGo(this));
        handles.put("bye", new HandlerBye(this));
        handles.put("help",new HandlerHelp(this));
        createRooms();
    }

    public void createRooms()
    {
       
        Room outside, lobby, pub, study, bedroom;

        //	制造房间
        outside = new Room("城堡外");
        lobby = new Room("大堂");
        pub = new Room("小酒吧");
        study = new Room("书房");
        bedroom = new Room("卧室");

        //	初始化房间的出口
        outside.setExit("east",lobby);
        outside.setExit("south",study);
        outside.setExit("west",pub);
        lobby.setExit("west",outside);
        pub.setExit("east",outside);
        study.setExit("north",outside);
        study.setExit("east",bedroom);
        bedroom.setExit("west",study);
        lobby.setExit("up",pub);
        pub.setExit("down",lobby);

        currentRoom = outside;  //	从城堡门外开始
    }

    public void printWelcome() {
        System.out.println();
        System.out.println("欢迎来到城堡！");
        System.out.println("这是一个超级无聊的游戏。");
        System.out.println("如果需要帮助，请输入 'help' 。");
        System.out.println();
        showPrompt();
    }

    public void goRoom(String direction)
    {
        Room nextRoom = currentRoom.getExit(direction);
        if (nextRoom == null) {
            System.out.println("那里没有门！");
        }
        else {
            currentRoom = nextRoom;
            showPrompt();
        }
    }

    public void showPrompt(){
        System.out.println("现在你在" + currentRoom);
        System.out.print("出口有: ");
        System.out.print(currentRoom.getExitDesc());
        System.out.println();
    }

    public void initSence(){
        initPerson();
        initMonster();
    }
    public void initPerson(){
        Random random = new Random();
        person.setId("2000001");
        person.setDescription("mary");
        person.setHPValue(400);
        person.setCurrentWeapon(new WeaponSword("pw1","倚天剑",-(int)(60+Math.random()*(70-60+1))));
    }

    public void initMonster(){
        Random random = new Random();
        wolf1.setId("wolf1");
        wolf1.setDescription("狼1");
        wolf1.setHPValue(200);
        wolf1.setCurrentWeapon(new WeapenKnife("mw1","大刀1",-(int)(40+Math.random()*(60-40+1))));

        wolf2.setId("wolf2");
        wolf2.setDescription("狼2");
        wolf2.setHPValue(200);
        wolf2.setCurrentWeapon(new WeapenKnife("mw2","大刀2",-(int)(50+Math.random()*(70-40+1))));

        wolf3.setId("wolf3");
        wolf3.setDescription("狼3");
        wolf3.setHPValue(200);
        wolf3.setCurrentWeapon(new WeapenKnife("mw3","大刀3",-(int)(30+Math.random()*(50-40+1))));

        monsterWolfSet.getMonsterWolves().add(wolf1);
        monsterWolfSet.getMonsterWolves().add(wolf2);
        monsterWolfSet.getMonsterWolves().add(wolf3);
    }

    public void play(){
       Scanner in = new Scanner(System.in);
       initSence();
       while ( true ) {
           String line = in.nextLine();
           String[] words = line.split(" ");
           Handler handler = handles.get(words[0]);
           String value="";
           if (words.length>1){
               value = words[1];
               if (handler != null) {
                   handler.doCmd(value);
                   System.out.println("如果这里有妖怪，你可以选择打妖怪(例：chop wolf1)，也可以选择离开");
                   ui.dispalyStaus(this);
                   Scanner mo = new Scanner(System.in);
                   String command = mo.nextLine();
                   String[] word = command.split(" ");
                   if (word[0].equals("chop")) {
                       if (this.getPerson().getHPValue() <= 0) {
                           judgeSuccess();
                       } else {
                           MonsterWolf monsterWolf = this.getMonsterWolfSet().get(word[1]);
                           if (monsterWolf.getHPValue() <= 0) {
                               System.out.println("该狼已经死了，请选择其他狼进行攻击");
                               ui.displayError();
                           } else {
                               this.getPerson().useAritical(this.getPerson().getCurrentWeapon(), this.getMonsterWolfSet().get(word[1]));
                               ui.displayDamageMsg(this.getPerson(), this.getMonsterWolfSet().get(word[1]));

                               for (MonsterWolf item : this.getMonsterWolfSet().getMonsterWolves()) {
                                       if (item.getHPValue() > 0) {
                                           if (1 == Utils.randomMonsterChop()) {
                                               item.useAritical(item.getCurrentWeapon(), this.getPerson());
                                               ui.displayDamageMsg(item, this.getPerson());
                                           }
                                       }
                               }

                           }
                       }
                       judgeSuccess();
                       ui.dispalyStaus(this);
                       System.out.println("请离开这个房间");
                   } else if (word[0].equals("go")) {
                       for (MonsterWolf item : this.getMonsterWolfSet().getMonsterWolves()) {
                           if (item.getHPValue() > 0) {
                               if (1 == Utils.randomMonsterChop()) {
                                   item.useAritical(item.getCurrentWeapon(), this.getPerson());
                                   ui.displayDamageMsg(item, this.getPerson());
                               }
                           }
                       }
                       ui.dispalyStaus(this);
                       judgeSuccess();
                       value = word[1];
                       handler.doCmd(value);
                   }else {
                       System.out.println("输入错误，请重新输入");
                   }
               }
           }
           else if (words[0].equals("bye")){
               ui.displayBye();
               System.exit(0);
           }
           else if (words[0].equals("help")){
               handler.doCmd(value);
           }
           else {
               System.out.println("输入错误，请重新输入");
           }
       }
   }

    public boolean judgeSuccess(){
        boolean flagPersonSuccess=true;
        if(this.getPerson().getHPValue() <=0){
            ui.dispalyStaus(this);
            System.out.println("很遗憾你输了，欢迎下次再来玩");
            System.exit(0);
            flagPersonSuccess=false;
        }
        if(wolf1.getHPValue()<=0 && wolf2.getHPValue()<=0 && wolf3.getHPValue()<=0) {
            flagPersonSuccess=true;
            ui.dispalyStaus(this);
            System.out.println("恭喜你胜利了");
            System.exit(0);
        }
        return flagPersonSuccess;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public MonsterWolfSet getMonsterWolfSet() {
        return monsterWolfSet;
    }

    public void setMonsterWolfSet(MonsterWolfSet monsterWolfSet) {
        this.monsterWolfSet = monsterWolfSet;
    }
}